<?php

/**
 * This is the model class for table "{{src}}".
 *
 * The followings are the available columns in table '{{src}}':
 * @property integer $id
 * @property string $src
 * @property string $site
 * @property string $manager
 * @property integer $active
 * @property string $d_start
 * @property string $d_end
 * @property string $d_cr
 */
class Src extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Src the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{src}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('src', 'required'),
			array('active', 'numerical', 'integerOnly'=>true),
			array('src', 'length', 'max'=>300),
			array('site, manager, d_start, d_end, d_cr', 'length', 'max'=>100),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, src, site, manager, active, d_start, d_end, d_cr', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'src' => 'Материал',
			'site' => 'Сайт',			
			'active' => 'Включено',
			'd_start' => 'Дата начала',
			'd_end' => 'Дата окончания',
			'manager' => 'Менеджер',
			'd_cr' => 'Дата создания',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('src',$this->src,true);
		$criteria->compare('site',$this->site,true);
		$criteria->compare('manager',$this->manager,true);
		$criteria->compare('active',$this->active);
		$criteria->compare('d_start',$this->d_start,true);
		$criteria->compare('d_end',$this->d_end,true);
		$criteria->compare('d_cr',$this->d_cr,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	protected function beforeSave() 
	{
		if(parent::beforeSave()) 
		{
			$src = $this->find('src=:src', array(':src'=>$this->src));
			if ($src) { 
				Yii::app()->user->setFlash('double', "Добавление существующего материала!");
				return false; 
			}
			if ($this->isNewRecord) { $this->d_cr = new CDbExpression('NOW()'); }
			if ($this->d_start) { $this->d_start = date('Y-m-d', strtotime($this->d_start)); }
			if ($this->d_end) { $this->d_end = date('Y-m-d', strtotime($this->d_end)); }	
			return true;
		} else { return false; }
	}

	protected function afterFind() 
	{
		if ($this->d_start) { 
			$date_d_start = date('d.m.Y', strtotime($this->d_start)); 
			$this->d_start = $date_d_start; 
		}
		if ($this->d_end) { 
			$date_d_end = date('d.m.Y', strtotime($this->d_end));
			$this->d_end = $date_d_end;
	   }
	   parent::afterFind();
	}	
	
	protected function beforeDelete() 
	{
		if (parent::beforeDelete()) {
			$words = Words::model()->findAllByAttributes(array('src'=>$this->src));		
			foreach($words as $word) {
				if ($word) {
					@unlink(Yii::getPathOfAlias('webroot.images.uploads').'/'.$word->source .'.html');
					@unlink(Yii::getPathOfAlias('webroot.images.files').'/'.$word->source_ban_f);
					@unlink(Yii::getPathOfAlias('webroot.images.files').'/'.$word->source_vid_f);
					//@unlink(Yii::getPathOfAlias('webroot.images.files').'/'.$word->screen_vid_f);
					@unlink(Yii::getPathOfAlias('webroot.images.files').'/'.$word->screen_vid);
					$word->delete();				
				}
			}
			return true;
		} else { return false; }
	} 	
}